<?php

/**
 * parser
 *
 * @author Admin
 */
class Parser extends Model_Abstract
{
    protected $_table_name = 'parser';
	
	protected $_parser_row = null;
	
	
	public function getParserForUpdate( $date = false )
	{
		if( !$this->_parser_row )
		{
			$sql = "SELECT * FROM " . $this->_table_name . " WHERE parsing=1 ". ( $date ? "AND next_parsing='". $date ."'" : $date) ." ORDER BY next_parsing ASC LIMIT 1";

			$this->_parser_row = $this->_db->get_row($sql);
		}
		
		return $this->_parser_row;
	}

	public function get( $params )
	{
		$params = $this->_glueKeyValue( $params );
	
		$sql = "SELECT * FROM " . $this->_table_name . " WHERE ". $params ." LIMIT 1";
		
		$this->_parser_row = $this->_db->get_row($sql);
		
		return $this->_parser_row;
	}
	
    public function insert( $group_id, $parser_name, $url, $parsing = 0, $next_parsing = null )
    {
		$fields = array(
			'group_id' => $group_id,
			'parser_name' => $parser_name,
			'url' => $url,
			'parsing' => $parsing,
			'next_parsing' => $next_parsing
		);

		$fields = $this->_paramsToString($fields);

		$sql = "INSERT INTO " . $this->_table_name . " SET " . $fields;

		$this->_db->get_results($sql);

		return $this->_getLastInsertId();
    }
    
    public function update( $id, $parser_name = false, $url = false, $parsing = false, $next_parsing = false )
    {
		$fields = array(
			'parser_name' => $parser_name,
			'url' => $url,
			'parsing' => $parsing,
			'next_parsing' => $next_parsing
		);

		$fields = $this->_paramsToString($fields);

		$sql = "UPDATE " . $this->_table_name . " SET " . $fields . " WHERE id = $id";

		return $this->_db->get_results($sql);
    }
    
   
}